<template>
  <div style="margin-left:6px;margin-right:6px;">
    <el-button
      v-bind="$attrs"
      class="filter-item"
      size="mini"
      type="primary"
      icon="el-icon-folder-checked"
      @click="changeCharger">更改负责人</el-button>
    <el-dialog :visible.sync="dialog" title="更改负责人" append-to-body width="400px">
      <el-form size="mini" label-width="100px">
        <el-form-item label="选择用户">
          <el-input :readonly="true" v-model="newChargerName" placeholder="请选择用户">
            <el-button slot="append" icon="el-icon-search" @click="selectUser" />
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button type="primary" @click="doMultiChangeCharger">确认</el-button>
      </div>
    </el-dialog>
    <userSelectList ref="userSelectList" :multi_select="false" @userSelectCallback="userSelectCallback" />
  </div>
</template>

<script>
import userSelectList from '@/views/system/user/module/userSelectList'

export default {
  name: 'ChangeCharger',
  components: { userSelectList },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      dialog: false,
      newChargerId: null,
      newChargerName: null
    }
  },
  methods: {
    cancel() {
      this.dialog = false
    },
    selectUser() {
      const _this = this.$refs.userSelectList
      _this.loadData()
      _this.dialog = true
    },
    userSelectCallback(selectedRow, userSelectObj) {
      this.newChargerId = selectedRow.id
      this.newChargerName = selectedRow.userName
      userSelectObj.cancel()
      userSelectObj.loading = false
    },
    changeCharger() {
      const _thisParent = this.sup_this
      const selectIds = []
      _thisParent.selectedGridRows.forEach(row => {
        selectIds.push(row.id)
      })
      if (selectIds.length === 0) {
        this.$message({
          message: '请选择记录',
          type: 'warning'
        })
        return
      }
      this.dialog = true
    },
    doMultiChangeCharger() {
      const _thisParent = this.sup_this
      const selectIds = []
      _thisParent.selectedGridRows.forEach(row => {
        selectIds.push(row.id)
      })
      this.cancel()
      this.$emit('doMultiChangeCharger', selectIds, this.newChargerId)
    }
  }
}
</script>

